﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.IO;
using MySql.Data.MySqlClient;
using System.Threading;

namespace SimoBotGUI
{
    public partial class frmMain : Form
    {
        //private frmConnect ConnectInfo;
        public frmMain()
        {
            InitializeComponent();
            bgwMsgLoop.WorkerReportsProgress = true;

        }

        public void AddToLsbText(string text)
        {
            lsbText.Items.Add(text); 
            lsbText.TopIndex = lsbText.Items.Count - 1; // Autoscrolls the listbox...
        }

        private void frmMain_Load(object sender, EventArgs e)
        {
        }

        string server;
        int port;
        string channel;
        string nick;
        public void DerpText(string Server, int Port, string Channel, string Nick) //this function is triggered from frmConnect.cs.
        {
            lsbText.Items[0] = Server;
            lsbText.Items[1] = Port.ToString();
            lsbText.Items[2] = Channel;
            lsbText.Items[3] = Nick;
            lsbText.SelectedItem = lsbText.Items.Count - 1; //Scroll the listbox
            server = Server;
            port = Port;
            channel = Channel;
            nick = Nick;
        }

        private void JokuDerp_Click(object sender, EventArgs e)
        {
            frmConnect ConnectFrm = new frmConnect(this);
            ConnectFrm.ShowDialog();
        }

        private void btnConnect_Click(object sender, EventArgs e)
        {
            if (bgwMsgLoop.IsBusy != true)
            {
                bgwMsgLoop.RunWorkerAsync();
            }
        }

        private SimoBotEngine engine;
        private void bgwMsgLoop_DoWork(object sender, DoWorkEventArgs e)
        {
            BackgroundWorker worker = sender as BackgroundWorker;
            SimoBotEngine engineAsd = new SimoBotEngine(server, port, channel, nick, this, worker); //No fun stuff in SimoBotEngine's constructor. Don't worry.
            engine = engineAsd;
            engine.initConnect();

            
        }
        string LastMsg = "lol";
        private bool IsNewMsg(string newMsg)
        {
            if (LastMsg == newMsg)
            {
                return false;

            }
            else
            {
                LastMsg = newMsg;
                return true;
            }
        }

        private void bgwMsgLoop_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            string TimeStampedMsg = engine.GetSharedMessage();
            int i = 0;
            for (bool a = false;a==false && i<15;)
            {
                TimeStampedMsg = engine.GetSharedMessage();
                a = IsNewMsg(TimeStampedMsg);
                i++;
                Thread.Sleep(10);
            }
            
            string[] splitMsg = TimeStampedMsg.Split(' ');
            string splittedTime = splitMsg[splitMsg.Length - 1];

            string StamplessMsg = TimeStampedMsg.Replace(splittedTime, "");
            lsbText.Items.Add(StamplessMsg);

            
        }

        private void bgwMsgLoop_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {

        }

        private void btnUserMsg_Click(object sender, EventArgs e)
        {

        }
    }
}